<pngx-page-header
  title="Users & Groups"
  i18n-title
  info="Create, delete and edit users and groups."
  i18n-info
  infoLink="usage/#users-and-groups"
  >
</pngx-page-header>

@if (users) {
  <h4 class="d-flex">
    <ng-container i18n>Users</ng-container>
    <button type="button" class="btn btn-sm btn-outline-primary ms-4" (click)="editUser()" *pngxIfPermissions="{ action: PermissionAction.Add, type: PermissionType.User }">
      <i-bs name="plus-circle"></i-bs>&nbsp;<ng-container i18n>Add User</ng-container>
    </button>
  </h4>
  <ul class="list-group">
    <li class="list-group-item">
      <div class="row">
        <div class="col" i18n>Username</div>
        <div class="col" i18n>Name</div>
        <div class="col" i18n>Groups</div>
        <div class="col" i18n>Actions</div>
      </div>
    </li>
    @for (user of users; track user) {
      <li class="list-group-item">
        <div class="row">
          <div class="col d-flex align-items-center"><button class="btn btn-link p-0 text-start" type="button" (click)="editUser(user)" [disabled]="!permissionsService.currentUserCan(PermissionAction.Change, PermissionType.User)">{{user.username}}</button></div>
          <div class="col d-flex align-items-center">{{user.first_name}} {{user.last_name}}</div>
          <div class="col d-flex align-items-center">{{user.groups?.map(getGroupName, this).join(', ')}}</div>
          <div class="col">
            <div class="btn-group">
              <button class="btn btn-sm btn-outline-secondary" type="button" (click)="editUser(user)" *pngxIfPermissions="{ action: PermissionAction.Change, type: PermissionType.User }">
                <i-bs width="1em" height="1em" name="pencil"></i-bs>&nbsp;<ng-container i18n>Edit</ng-container>
              </button>
              <button class="btn btn-sm btn-outline-danger" type="button" (click)="deleteUser(user)" *pngxIfPermissions="{ action: PermissionAction.Delete, type: PermissionType.User }">
                <i-bs width="1em" height="1em" name="trash"></i-bs>&nbsp;<ng-container i18n>Delete</ng-container>
              </button>
            </div>
          </div>
        </div>
      </li>
    }
  </ul>
}

@if (groups) {
  <h4 class="mt-4 d-flex">
    <ng-container i18n>Groups</ng-container>
    <button type="button" class="btn btn-sm btn-outline-primary ms-4" (click)="editGroup()" *pngxIfPermissions="{ action: PermissionAction.Add, type: PermissionType.Group }">
      <i-bs name="plus-circle"></i-bs>&nbsp;<ng-container i18n>Add Group</ng-container>
    </button>
  </h4>
  <ul class="list-group">
    <li class="list-group-item">
      <div class="row">
        <div class="col" i18n>Name</div>
        <div class="col"></div>
        <div class="col"></div>
        <div class="col" i18n>Actions</div>
      </div>
    </li>
    @for (group of groups; track group) {
      <li class="list-group-item">
        <div class="row">
          <div class="col d-flex align-items-center"><button class="btn btn-link p-0 text-start" type="button" (click)="editGroup(group)" [disabled]="!permissionsService.currentUserCan(PermissionAction.Change, PermissionType.Group)">{{group.name}}</button></div>
          <div class="col"></div>
          <div class="col"></div>
          <div class="col">
            <div class="btn-group">
              <button class="btn btn-sm btn-outline-secondary" type="button" (click)="editGroup(group)" *pngxIfPermissions="{ action: PermissionAction.Change, type: PermissionType.Group }">
                <i-bs width="1em" height="1em" name="pencil"></i-bs>&nbsp;<ng-container i18n>Edit</ng-container>
              </button>
              <button class="btn btn-sm btn-outline-danger" type="button" (click)="deleteGroup(group)" *pngxIfPermissions="{ action: PermissionAction.Delete, type: PermissionType.Group }">
                <i-bs width="1em" height="1em" name="trash"></i-bs>&nbsp;<ng-container i18n>Delete</ng-container>
              </button>
            </div>
          </div>
        </div>
      </li>
    }
    @if (groups.length === 0) {
      <li class="list-group-item" i18n>No groups defined</li>
    }
  </ul>
}

@if (!users || !groups) {
  <div>
    <div class="spinner-border spinner-border-sm fw-normal ms-2 me-auto" role="status"></div>
    <div class="visually-hidden" i18n>Loading...</div>
  </div>
}
